import { saveAs } from "file-saver";

let downloadStrategies = {
  iframe: function (src) {
    console.log(`iframe下载策略`);
    return new Promise((resolve, reject) => {
      let iframe = document.createElement("iframe");
      iframe.src = src;
      // 隐藏iframe
      iframe.hidden = true;
      iframe.style.width = 0;
      iframe.style.height = 0;
      iframe.onload = function () {
        document.body.removeChild(iframe);
        iframe = null;
        resolve();
      };
      iframe.onerror = function () {
        reject();
      };
      document.body.appendChild(iframe);
    });
  },

  fetch: function (src, fileName) {
    console.log(`fetch下载策略`);
    return fetch(src)
      .then((response) => response.blob())
      .then((blob) => {
        saveAs(blob, fileName, {});
      });
  },
};

export { downloadStrategies };
